var ns,UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        CompleteDesc:'',
        writeCount:0,
        imglist:[],
        imgindex: 0,
        select_imglist: [],
        select_imgindex: 0,
        maxuploadcount: 0,
        show:false,
        placepoint:[],
        placeid:0,//配送点id
        placename:'',//收货配送点
    },
    methods: {
        get_data:function(){
            var that=this;
            var options={};
            options.action='getorderpointlist';
            options.ID=that.ID;
            console.log(JSON.stringify(options));
            ns.post(options, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        that.placepoint=data.data.PointList;
                        that.placeid=that.placepoint[0].ID;
                        that.placename=that.placepoint[0].SendPoint;
                    }

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
            
        },
        writeMsg:function(){
            var that=this;
            var testVal=that.CompleteDesc.length;
            that.writeCount=testVal;
        },
        handOpenplace:function(){
            var that=this;
            that.show=true

        },
        choosepoint:function(item){
            var that=this;
            that.placeid=item.ID;
            that.placename=item.SendPoint;
            that.show=false;
        },
        //选择图片
        choose_picture: function (num) {
            var that = this;
            that.maxuploadcount = num;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },

        open_save:function(){
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            var option={};
            option.ID=that.ID;
            option.PointID=that.placeid;
            option.PointName=that.placename;
            option.PlantFormReceiveRemark=that.CompleteDesc;
            option.action='acceptorderpoint';
            console.log(JSON.stringify(option));
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name:'shopmsgevent'
                        });
                        setTimeout(() => {
                            api.closeWin();
                        }, 300);
                    }else{
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                files: {
                    file: filelist
                }
            })
        },
        // 图片预览
        // do_openimages: function () {
        //     var that = this;
        //     var imageUrls = [];
        //     imageUrls.push(that.ImagePath);
        //     var imageBrowser = api.require('imageBrowser');
        //     imageBrowser.openImages({
        //         showList: false,
        //         imageUrls: imageUrls
        //     });
        // },
        //选择多张-商品详情图
        choose_picture_start: function () {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function (ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
            return;
        },
        UIAlbumBrowser_transPath: function () {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function (ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },
        remove_img: function (index) {
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                }
            }

        },
        // 查看商品详情图
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.ID = api.pageParam.id;
    app.get_data();
};
